<template>
  <div class="login-container">
    <el-form
      ref="loginFormRef"
      class="login-form"
      auto-complete="on"
      label-position="left"
      :model="loginForm"
      :rules="loginRules"
    >
      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/common/login-logo.png" alt="logo">
        </h3>
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <!-- 用户名输入框 -->
        <el-input
          v-model.trim="loginForm.mobile"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="off"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <!-- 密码输入框 -->
        <el-input
          ref="password"
          :key="passwordType"
          v-model.trim="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button
        class="login-btn"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click="handleLogin"
      >登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">账号: 13800000002</span>
        <span>密码: hm#qd@23!</span>
      </div>
    </el-form>
  </div>
</template>

<script>

export default {
  name: 'LoginView',
  data() {
    return {
      // 密码框类型
      passwordType: 'password',
      loginForm: {
        mobile: '13800000002',
        password: 'hm#qd@23!'
      },
      // 表单校验规则
      loginRules: {
        // 手机号的校验规则
        mobile: [
          { required: true, message: '手机号必填', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的格式', trigger: 'blur' }
        ],
        // 密码的校验规则
        password: [
          { required: true, message: '密码必填', trigger: 'blur' }
          // { pattern: /^\d{6}$/, message: '请输入正确的密码', trigger: 'blur' }
          // { pattern: /^\d{6}$/, message: '请输入正确的密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      // 整体校验
      // console.log(this.$refs.loginFormRef)
      this.$refs.loginFormRef.validate(async valid => {
        // valid：表示所有的表单项是否都通过了校验
        // 如果是：值为 true,否则是 false

        // 没通过的话提前结束
        if (!valid) return
        // 通过了后调接口发请求
        // await 只能提取到成功态的Promise结果
        // 如果想要接口失败时候不在控制台显示红色的提示，
        // 并且还想拿到失败的错误信息
        // 需要对接口函数手动进行 try-catch 异常捕获
        // 管理token
        // 跳转至首页
        try {
          await this.$store.dispatch('user/loginAction', this.loginForm)
          this.$message.success('登录成功！')
          // 登录成功跳转至：优先跳转至回跳地址，其次首页
          // this.$router.replace('/')
          this.$router.replace(this.$route.query.redirectUrl || '/')
        } catch (error) {
          this.$message({
            type: 'error',
            message: error.message || '登录失败！'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
// 样式变量
$bg: #283443;
$dark_gray: #889aa4;
$light_gray: #407ffe;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $light_gray;
  }
}

// 登录容器
.login-container {
  background-image: url('~@/assets/common/login.jpg'); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $light_gray;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 10px;
    margin: 0 auto;
    overflow: hidden;
    border:2px solid red;
    .login-btn {
      background: #407ffe;
      height: 64px;
      line-height: 32px;
      font-size: 24px;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 auto 30px;
      text-align: center;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.9); // 输入登录表单的背景色
    border-radius: 5px;
    color: $light_gray;
    .el-form-item__error {
      color: #fff;
      font-size: 14px;
    }
  }
}
</style>
<style>
.el-input .el-input__inner{
  margin-left:10px !important;
  margin-top:8px;
}
</style>
